//
//  MMGPrizeClawGameControlHandler.cpp
//  DollsVending
//
//  Created by Don on 2017/11/2.
//  Copyright © 2017年 Memego, Inc. All rights reserved.
//

#include "MMGPrizeClawGameControlHandler.hpp"


static MMGPrizeClawGameControlHandler* gcInstance;
MMGPrizeClawGameControlHandler* MMGPrizeClawGameControlHandler::getInstance() {
    
    if (gcInstance == NULL)
    {
        gcInstance = new MMGPrizeClawGameControlHandler();
        
    }
    
    return gcInstance;
    
}

MMGPrizeClawGameControlHandler::MMGPrizeClawGameControlHandler() {
}

MMGPrizeClawGameControlHandler::~MMGPrizeClawGameControlHandler() {
    gcInstance = NULL;
}

void MMGPrizeClawGameControlHandler::initGameproto() {
    gameproto_init(onControlMessageDidReceive);
}
void MMGPrizeClawGameControlHandler::uninitGameproto() {
    gameproto_uninit();
}
void MMGPrizeClawGameControlHandler::gameprotoSendmessage(NSString *command, NSString *data, int code) {
    const char *com = NULL;
    const char *dat = NULL;
    if (!NSStringIsNullOrEmpty(command)) {
        com = [command UTF8String];
    }
    if (!NSStringIsNullOrEmpty(data)) {
        dat = [data UTF8String];
    }
    
    //
    const char *appVersion = [[NSString stringWithFormat:@"ios:%@",kMMG_SYS_APP_VERSION] UTF8String];
    
    gameproto_sendmessage(com, dat, code, appVersion);
}
void MMGPrizeClawGameControlHandler::gameprotoSendcontrol(int ctrl, NSString *param) {
    const char *par = NULL;
    if (!NSStringIsNullOrEmpty(param)) {
        par = [param UTF8String];
    }
    gameproto_sendcontrol(ctrl, par);
}

//注册 回调函数
void MMGPrizeClawGameControlHandler::registerGameControlMessageDidReceiveCallBackFunction(void *ref, MMGGameControlMessageDidReceiveHandler handler) {
    if (this->ref) {
        this->ref = NULL;
    }
    if (this->messageDidReceiveHandler) {
        this->messageDidReceiveHandler = NULL;
    }
    
    this->ref = ref;
    this->messageDidReceiveHandler = handler;
    
}

#pragma mark - prize claw game control message did receive callback
void MMGPrizeClawGameControlHandler::onControlMessageDidReceive(const char *command, const char *data, int code) {
    if (gcInstance && gcInstance->ref) {
        
        if (gcInstance->messageDidReceiveHandler != NULL) {
            gcInstance->messageDidReceiveHandler(gcInstance->ref,command,data,code);
        }
    }
}
